//
//  MainTabBarController.m
//  HuiWenMovie
//
//  Created by ios on 15/7/19.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "MainTabBarController.h"
#import "MovieViewController.h"
#import "NewsViewController.h"
#import "TopViewController.h"
#import "CinemaViewController.h"
#import "MoreViewController.h"
#import "HWButton.h"
#import "BaseNavigationController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewControllers];
    [self setTabBar];
    
    
    // Do any additional setup after loading the view.
}

- (void)setSubViewControllers
{
    MovieViewController *movieViewControllers = [[MovieViewController alloc]init];
    NewsViewController *newsViewControllers = [[NewsViewController alloc]init];
    TopViewController *topViewControllers = [[TopViewController alloc]init];
    CinemaViewController *cinemaViewControllers = [[CinemaViewController alloc]init];
    MoreViewController *moreViewControllers = [[MoreViewController alloc]init];
    
    NSArray *vcs = @[movieViewControllers,newsViewControllers,topViewControllers,cinemaViewControllers,moreViewControllers];
    NSMutableArray *navs = [[NSMutableArray alloc]init];
    
    for(int i =0;i < 5;i++)
    {
        BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:vcs[i]];
        [navs addObject:nav];
    }
    
    self.viewControllers = navs;
    

}

- (void)setTabBar
{
    for (UIView *subView in self.tabBar.subviews) {
        Class cls = NSClassFromString(@"UITabBarButton");
        
        if([subView isKindOfClass:cls])
        {
            [subView removeFromSuperview];
        }
    }
    
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    
    CGFloat width = CGRectGetWidth(self.tabBar.frame)/5;
    CGFloat height = CGRectGetHeight(self.tabBar.frame);
    NSArray *imageName =@[@"movie_home.png",@"msg_select_new.png",@"start_top250.png",@"icon_cinema.png",@"more_setting.png"];
    NSArray *titles = @[@"电影",@"新闻",@"Top",@"影院",@"更多"];
    selectedImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    selectedImage.image = [UIImage imageNamed:@"selectTabbar_bg_all.png"];
    
    [self.tabBar addSubview:selectedImage];
    
//    for(int i = 0;i < 5;i++)
//    {
//        UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(i*width, 0, width, height)];
//        [but setImage:[UIImage imageNamed:imageName[i]] forState:UIControlStateNormal];
//        [but setTitle:titles[i] forState:UIControlStateNormal];
//        [self.tabBar addSubview:but];
//        but.titleLabel.font = [UIFont systemFontOfSize:13];
//        
//        [but setTitleEdgeInsets:UIEdgeInsetsMake(30, -18, 0, 0)];
//        [but setImageEdgeInsets:UIEdgeInsetsMake(-10, 20, 0, 0)];
//    
//    }
    for(int i = 0;i < 5;i++)
    {
        CGRect frame = CGRectMake(i*width, 0, width, height);
        HWButton *bt = [[HWButton alloc]initWithFrame:frame AndImageName:imageName[i] WithTitleName:titles[i]];
        bt.tag = i;
        [bt addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:bt];
    }
    self.tabBar.translucent = YES;
    

}

- (void)ButtonAction:(HWButton *)button
{
    self.selectedIndex = button.tag;
    [UIView beginAnimations:nil context:nil];
    selectedImage.frame = button.frame;
    [UIView setAnimationDuration:0.2];
    [UIView commitAnimations];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
